<template>
  <div>
    <a-drawer
      :maskClosable="false"
      :title="title"
      :width="width"
      placement="right"
      :closable="true"
      :bodyStyle="bodyStyle"
      style="top: 0px"
      @close="close"
      :visible="visible">
      <erp-hr-post-salary-adjustment-form ref="realForm" @ok="submitCallback" :disabled="disableSubmit" normal></erp-hr-post-salary-adjustment-form>
      <div class="drawer-footer">
        <a-popconfirm title="确定关闭弹窗吗?" @confirm="handleCancel" :getPopupContainer="(node) =>  node.parentNode">
          <a-button style="margin-bottom: 0;">关闭</a-button>
        </a-popconfirm>
        <a-button v-if="!disableSubmit"  @click="handleOk(true)" type="primary" style="margin-bottom: 0;">发起流程</a-button>
        <a-button v-if="!disableSubmit"  @click="handleOk(false)" type="primary" style="margin-bottom: 0;">保存草稿</a-button>
      </div>
    </a-drawer>
    <a-modal :visible="selectFlag" title="调岗调薪审批单提示" @ok="okTemp" @cancel="cancelTemp" :closable="false" cancelText="放弃编辑" okText="继续编辑">
      <a-alert
        description="您当前存在未提交的审批单信息，是否继续编辑？点击【放弃编辑】按钮新增审批单信息。"
        type="warning"
        show-icon
      />
    </a-modal>
    <a-modal :visible="flag" title="调岗调薪审批单提示" :closable="false" >
      <a-alert
        :description="description"
        type="warning"
        show-icon
      />
      <template slot="footer">
        <a-button @click.stop="()=>{flag=false}">关闭</a-button>
      </template>
    </a-modal>
    </div>
</template>

<script>

  import ErpHrPostSalaryAdjustmentForm from './ErpHrPostSalaryAdjustmentForm'
  import { httpAction, getAction,postAction } from '@/api/manage'
  import { mapGetters } from 'vuex';

  export default {
    name: 'ErpHrPostSalaryAdjustmentModal',
    components: {
      ErpHrPostSalaryAdjustmentForm
    },
    data () {
      return {
        description:"",
        model:{},
        flag:false,
        continueflag:false,
        selectFlag:false,
        title:"操作",
        width:'100%',
        visible: false,
        disableSubmit: false,
        bodyStyle: {
          padding: '20px 20px 70px',
          height: window.innerHeight - 56 + 'px',
          'overflow-y': 'auto',
        },
        url:{
          queryHandleAdjustmentByEmployeeId: '/api/erp/hr/adjust/erpHrPostSalaryAdjustment/queryHandleAdjustmentByEmployeeId',
          queryTempData: '/api/erp/hr/adjust/erpHrPostSalaryAdjustment/queryTempData',
          editAdjustmentByEmployeeId: '/api/erp/hr/adjust/erpHrPostSalaryAdjustment/editAdjustmentByEmployeeId',
        }
      }
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        let userinfo=this.userInfo();
        //判断是否已经审批成功了
        getAction(this.url.queryHandleAdjustmentByEmployeeId, {employeeId:userinfo.empId}).then((res) => {
          if (res.success) {
            let data=res.result
            if(data.handle>0){
              this.flag=true;
              this.description="您的审批单正在处理中，请勿重复提交审批！"
            }else{
              //判断是否存在未提交的审批单
              getAction(this.url.queryTempData, {employeeId:userinfo.empId}).then((res) => {
                if (res.success) {
                  let data=res.result.records
                  if(data[0]){
                    this.model=data[0];
                    this.selectFlag=true;
                  }else{
                    this.title="员工调岗调薪申请单新增"
                    this.visible=true
                    this.selectFlag=false;
                    this.continueflag=false;
                    this.$nextTick(()=>{
                      this.$refs.realForm.add();
                    }) 
                  }
                }
              })
              }
            }
        })
      },
      edit (record) {
        this.title="员工调岗调薪申请单编辑"
        this.visible=true
        this.$nextTick(()=>{
          this.$refs.realForm.edit(record);
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleOk (flag) {
        if(flag){
          this.$refs.realForm.submitForm();
        }else{
          this.$refs.realForm.submitTempForm();
        }
      },
      okTemp(){
        this.title="员工调岗调薪申请单编辑"
        this.visible=true;
        this.selectFlag=false;
        this.continueflag=true;
        this.$nextTick(()=>{
          this.$refs.realForm.edit(this.model);
        })
      },
      cancelTemp(){
        let userinfo=this.userInfo();
        getAction(this.url.editAdjustmentByEmployeeId, {employeeId:userinfo.empId}).then((res) => {
          if (res.success) {
            this.title="员工调岗调薪申请单新增"
            this.visible=true;
            this.selectFlag=false;
            this.continueflag=false;
            this.$nextTick(()=>{
              this.$refs.realForm.add();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
  .drawer-footer{
    position: absolute;
    bottom: 0px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
  // 兼容1.6.2版本的antdv全屏
  /deep/ .ant-modal-footer{
    height: 50px;
  }
</style>
